package test.xls;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.aat.dxfy.utils.POIUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;

public class ReadCityXls {

	public static void main(String[] args) throws Exception {
		File f = new File("D:\\City_data.js");
		InputStream ins = new FileInputStream(f);
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		int i = 0;
		byte[] bs = new byte[1024 * 10];
		while ((i = ins.read(bs)) != -1) {
			bos.write(bs, 0, i);
		}
		ins.close();
		byte[] resbs = bos.toByteArray();
		String res = new String(resbs);
		JSONObject isonobj = JSONObject.parseObject(res);
		System.out.println(isonobj.get("list"));
	}

	/*
	 * List<Map<Integer, String>> lists =
	 * POIUtil.readExcelContent("D:\\city.xls");
	 * 
	 * for (Map<Integer, String> map : lists) { // "010": ["北京", "BEIJING",
	 * "BJ"] String ss = "\"" + map.get(0) + "\":[\"" + map.get(4) + "\",\"" +
	 * fullPY(map.get(4)) + "\",\"" + FZMPY(map.get(4)) + "\"],";
	 * System.out.println(ss); System.out.println(map); }
	 */

	public static void test() throws IOException {
		List<Map<Integer, String>> lists = POIUtil.readExcelContent("D:\\city.xls");
		Set<String> p = new HashSet<>();
		Map<String, Set<String>> re = new HashMap<>();

		for (Map<Integer, String> map : lists) {
			// "010": ["北京", "BEIJING", "BJ"]
			// String ss = "\"" + map.get(0) + "\":[\"" + map.get(4) + "\",\"" +
			// fullPY(map.get(4)) + "\",\"" + FZMPY(map.get(4)) + "\"],";
			// System.out.println(ss);
			p.add(map.get(1));

		}

		for (String s : p) {
			Set<String> c = new HashSet<>();
			for (Map<Integer, String> map : lists) {
				if (map.get(1).equals(s)) {
					c.add(map.get(0));
				}
			}
			re.put(s, c);
		}

		System.out.println(JSON.toJSONString(p));
		System.out.println(JSON.toJSONString(re));
	}

	public static String fullPY(String str) throws Exception {
		HanyuPinyinOutputFormat outputFormat = new HanyuPinyinOutputFormat();
		outputFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
		outputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		char[] cs = str.toCharArray();
		String piny = "";
		for (char c : cs) {
			String[] ss = PinyinHelper.toHanyuPinyinStringArray(c, outputFormat);
			piny += ss[0];
		}
		return piny;
	}

	public static String FZMPY(String str) throws Exception {
		HanyuPinyinOutputFormat outputFormat = new HanyuPinyinOutputFormat();
		outputFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
		outputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		char[] cs = str.toCharArray();
		String piny = "";
		for (char c : cs) {
			String[] ss = PinyinHelper.toHanyuPinyinStringArray(c, outputFormat);
			piny += ss[0].substring(0, 1);
		}
		return piny;
	}

	/**
	
	 */

}
